/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.usc.cs523.client;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.input.InputManager;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.ArrayList;

/**
 *
 * @author haoxu
 */
public class PanelManager {

    private Nifty nifty;
    private NiftyJmeDisplay niftyDisplay;
    private Node guiNode;
    private AssetManager assetManager;
    private InputManager inputManager;
    private AudioRenderer audioRenderer;
    private ViewPort guiViewPort;
    private int cursor;
    private int number;
    private ScreenController app;
    protected TextRenderer HP;
    protected TextRenderer MP;
    protected ArrayList<TextRenderer> items;

    PanelManager(Node guiNode, AssetManager assetManager, InputManager inputManager, AudioRenderer audioRenderer, ViewPort guiViewPort, ClientMain app) {
        this.guiNode = guiNode;
        this.assetManager = assetManager;
        this.inputManager = inputManager;
        this.audioRenderer = audioRenderer;
        this.guiViewPort = guiViewPort;
        this.app = (ScreenController) app;
        initial();
    }

    private void initial() {
        niftyDisplay = new NiftyJmeDisplay(assetManager,
                inputManager,
                audioRenderer,
                guiViewPort);
        nifty = niftyDisplay.getNifty();
        try {
            nifty.fromXml("Interface/ClientUI.xml", "load_game", app);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        guiViewPort.addProcessor(niftyDisplay);

    }

    public void gotoGame() {
        nifty.gotoScreen("default_hud");
        HP = nifty.getScreen("default_hud").findElementByName("layer").findElementByName("panel_bottom").findElementByName("bottom_panel_left").findElementByName("status_text_01").getRenderer(TextRenderer.class);
        MP = nifty.getScreen("default_hud").findElementByName("layer").findElementByName("panel_bottom").findElementByName("bottom_panel_left").findElementByName("status_text_02").getRenderer(TextRenderer.class);

        items = new ArrayList<TextRenderer>();

        for (int i = 1; i <= 4; i++) {
            TextRenderer text = nifty.getScreen("default_hud").findElementByName("layer").findElementByName("panel_bottom").findElementByName("bottom_panel_right").findElementByName("status_text_0" + i).getRenderer(TextRenderer.class);
            text.setText("-----");
            items.add(text);
        }
        cursor = -1;
        number = 0;
    }

    public void gotoLoading() {
        nifty.gotoScreen("load_level");
    }

    public void setHP(String data) {
        this.HP.setText(data);
    }

    public void setMP(String data) {
        this.MP.setText(data);
    }

    public void addItem(String item) {
        number++;
        if (number == 1) {
            cursor = 0;
            items.get(number - 1).setText("->" + item);
        } else {
            items.get(number - 1).setText(item);
        }
    }

    public String useItem() {
        if (number > 0) {
            String item = items.get(cursor).getWrappedText().substring(items.get(cursor).getWrappedText().indexOf(">") + 1);
            items.get(cursor).setText("----");
            cursor = ++cursor % number;
            items.get(cursor).setText("->" + items.get(cursor).getWrappedText());
            return item;
        } else {
            return null;
        }
    }

    public void moveCursor() {
        if (number > 0) {
            String original = items.get(cursor).getWrappedText();
            String replace = original.substring(original.indexOf(">") + 1);
            items.get(cursor).setText(replace);
            cursor = ++cursor % number;
            items.get(cursor).setText("->" + items.get(cursor).getWrappedText());
        }
    }

    void setMessage(String string) {
        nifty.getScreen("default_hud").findElementByName("layer").findElementByName("panel_top").findElementByName("top_panel_left").findElementByName("status_text_01").getRenderer(TextRenderer.class).setText(string);
    }
}
